// Attach this to a GUIText to make a frames/second indicator.
//
// It calculates frames/second over each updateInterval,
// so the display does not keep changing wildly.
//
// It is also fairly accurate at very low FPS counts (<10).
// We do this not by simply counting frames per interval, but
// by accumulating FPS for each frame. This way we end up with
// correct overall FPS even if the interval renders something like

using UnityEngine;

[DisallowMultipleComponent]
public class FPSCounter : MonoBehaviour 
{
	public float updateInterval= 1.0f;

    private uint uframes;
	private float utimer;
	private float ufps;
	private float ultimer;

    private uint fframes;
    private float ftimer;
    private uint ffps;
    private float fltimer;

    private void Update()
    {
        utimer = Time.realtimeSinceStartup;
        if (utimer - updateInterval >= ultimer)
		{
            ufps = uframes;
            ultimer = utimer;
			uframes = 0;
		}
        ++uframes;
	}

    private void FixedUpdate()
    {
        ftimer = Time.realtimeSinceStartup;
        if (ftimer - updateInterval >= fltimer)
        {
            ffps = fframes;
            fltimer = ftimer;
            fframes = 0;
        }
        ++fframes;
    }

    private void OnGUI ()
    {
        string str = ufps.ToString("f2") + " | " + ffps.ToString("f2") + " TS " + Time.timeScale.ToString("f2");
        GUI.Box(new Rect(Screen.width - 300, 10, 300, 30), str + " | QSetting: " + QualitySettings.GetQualityLevel());
	}
}